#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

int main() {
  int fileDescriptor;
  ssize_t bytesRead, bytesWritten;
  off_t offset;
  char buffer[100];

  // 打开文件
  fileDescriptor = open("example.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  if (fileDescriptor == -1) {
    perror("open");
    exit(1);
  }

  // 写入数据
  bytesWritten = write(fileDescriptor, "Hello, World!", 13);
  if (bytesWritten == -1) {
    perror("write");
    exit(1);
  }

  // 设置偏移量
  offset = lseek(fileDescriptor, 0, SEEK_SET);
  if (offset == -1) {
    perror("lseek");
    exit(1);
  }

  // 读取数据
  bytesRead = read(fileDescriptor, buffer, sizeof(buffer) - 1);
  if (bytesRead == -1) {
    perror("read");
    exit(1);
  }
  buffer[bytesRead] = '\0';
  printf("Read from file: %s\n", buffer);

  // 关闭文件
  if (close(fileDescriptor) == -1) {
    perror("close");
    exit(1);
  }

  return 0;
}
